﻿using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp_MachineVision
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //ECC200二維碼識別（經測試，推薦用此方法，識別率高）
        static HDevelopExport_ECC200 hd = new HDevelopExport_ECC200();

        //ecc200二維碼
        static HDevelopExport_ECC200_Simple ecc200 = new HDevelopExport_ECC200_Simple();

        //qr碼識別
        //static HDevelopExport_QR qr = new HDevelopExport_QR();
        static HDevelopExport_QR02 qr = new HDevelopExport_QR02();

        private void button1_Click(object sender, EventArgs e)
        {
            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                try
                {
                    //HDevelopExport_QR qr = new HDevelopExport_QR();
                    HTuple _window = hWindowControl1.HalconWindow;
                
                    //識別字母
                    //HDevelopExport2 hd = new HDevelopExport2();
                    //hd.RunHalcon(_window);

                    //ecc200二維碼 
                    //C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_cpu_001.png
                    //hd.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_cpu_001.png";
                    //背景黑暗
                    //hd.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_cpu_006.png";
                    //hd.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_disturbed_008.png";
                    //光纖干擾識別參數配置和小尺寸識別參數不兼容，互相導致識別不出
                    //hd.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_contrast_variations_05.png";
                    //小尺寸識別
                    //hd.ImgPath = "d:/Desktop/2D_code/ecc200/ECC200_03.jpg";
                    //hd.ImgPath = "d:/Desktop/2D_code/ecc200/ECC200_05.jpg";
                    //hd.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/gs1datamatrix/gs1datamatrix_generated_01.png";
                    //干擾多，識別報錯  
                    //hd.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_damaged_finder_pattern_06.png";
                    //光纖很暗黑
                    //hd.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_disturbed_011.png";
                    //有干擾
                    //hd.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_disturbed_017.png";
                    //點狀
                    //hd.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_to_preprocess_003.png";
                    //hd.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_to_preprocess_004.png";
                    //hd.ImgPath = textBox1.Text.Trim()?.Replace(@"\","/");
                    //hd.RunHalcon(_window);

                    //ecc200二維碼
                    HDevelopExport_ECC200_Simple ecc200 = new HDevelopExport_ECC200_Simple();
                    ////ecc200.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_disturbed_017.png";
                    ////ecc200.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_disturbed_008.png";
                    //ecc200.ImgPath = "d:/Desktop/2D_code/ecc200/ECC200_03.jpg";
                    //點狀
                    //ecc200.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_to_preprocess_003.png";
                    //模糊，灰色背景
                    //ecc200.ImgPath = "C:/Users/Public/Documents/MVTec/HALCON-18.05-Progress/examples/images/datacode/ecc200/ecc200_to_preprocess_004.png";
                    //ecc200.RunHalcon(_window);

                    //qr code識別
                    qr.ImgPath = textBox1.Text.Trim()?.Replace(@"\", "/");
                    qr.RunHalcon(_window);
                }
                catch (Exception ex)
                {
                    ShowMsg($"異常：{ex.Message} {ex.InnerException?.Message}");
                }

                stopwatch.Stop();
                ShowMsg($"識別耗時：{stopwatch.ElapsedMilliseconds}毫秒");
            });
        }

        /// <summary>
        /// 顯示識別信息
        /// </summary>
        /// <param name="msg"></param>
        public static void ShowMsg(string msg)
        {
            var formObj = Program.form1.richTextBox1;
            formObj.BeginInvoke(new Action(() =>
            {
                if (string.IsNullOrEmpty(formObj.Text))
                {
                    formObj.AppendText(msg);
                }
                else
                {
                    formObj.AppendText("\r"+msg);
                }               
            }));
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
    }
}
